package classroom.edu.info.manager.controller;

import classroom.edu.info.manager.domain.Teacher;
import classroom.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    Scanner s = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();
    public void start() {
        Scanner s = new Scanner(System.in);
        lo:while (true) {
            System.out.println("-------欢迎来到老师管理系统--------");
            System.out.println("请输入您的选择：1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            System.out.println("请输入您的选择：");
            String choice = s.nextLine();
            switch (choice) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    deleteTeacher();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    selectTeacher();
                    break;
                case "5":
                    System.out.println("即将回到主菜单");
                    break lo;
                default:
                    System.out.println("您输入的信息有误，请重新输入.");

            }
        }
    }

    /**
     * 增加老师信息方法
     */
    private void updateTeacher() {
        //判断老师数组是否为空，如果为空结束方法
        if (isArrayNull()){ return;}
        //键盘输入老师id，判断老师是否存在
        String id = getId();
        //创建老师对象，调用inpTeacherInfo方法，通过键盘输入将信息赋值给对象
        Teacher newTeacher = inputTeacherInfo(id);
        //调用逻辑层update方法将newTeacher对象传递
        teacherService.updateTeacher(newTeacher);
        System.out.println("更改老师信息成功。");
    }

    /**
     * 删除老师信息方法
     */
    private void deleteTeacher() {
        //判断数组是否为空，如果为空结束方法
        if (isArrayNull()){ return;}
        //键盘输入老师id，判断老师是否存在
        String id = getId();
        //调用逻辑层方法
        teacherService.deleteTeacher(id);
        System.out.println("删除老师信息成功");
    }

    /**
     * 键盘录入老师工号，如果不存在就输入直到输到老师工号存在
     * @return 如果老师存在数组中，将老师工号返回
     */
    private String getId() {
        String id;
        while (true) {
            System.out.println("请输入老师工号：");
            id = s.next();
            boolean exists = teacherService.isExists(id);
            if (!exists){
                System.out.println("您输入的工号不存在，请重新输入.");
            }else {
                break;
            }
        }
        return id;
    }

    /**
     *
     * @return 判断老师数组是否为空 ，true表示空， false表示非空
     */
    private boolean isArrayNull() {
        boolean isnull = teacherService.isNull();
        if (!isnull){
            System.out.println("请先添加老师信息后在进行操作。");
            return true;
        }
        return false;
    }

    private void selectTeacher() {
        Teacher[] teacher = teacherService.selectTeacher();
        if (teacher == null){
            System.out.println("请先录入老师信息后在进行查询.");
            return;
        }
        System.out.println("老师信息如下：");
        System.out.println("工号\t姓名\t年龄\t工作时长");
        for (int i = 0; i < teacher.length; i++) {
            if (teacher[i]!=null) {
                System.out.println(teacher[i].getId() + "\t" + teacher[i].getName() + "\t" + teacher[i].getAge() + "\t" + teacher[i].getYear());
            }
        }
    }

    private void addTeacher() {
        String id;
        while (true) {
            System.out.println("请输入老师工号：");
            id = s.next();
            boolean exists = teacherService.isExists(id);
            if (exists){
                System.out.println("您输入的工号已存在，请重新输入.");
            }else {
                break;
            }
        }
        Teacher newTeacher = inputTeacherInfo(id);
        boolean result = teacherService.addTeacher(newTeacher);
        if (result){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }
    private Teacher inputTeacherInfo(String id){
        System.out.println("请输入老师名字：");
        String name = s.next();
        System.out.println("请输入老师年龄：");
        String age = s.next();
        System.out.println("请输入老师工作时长：");
        String year = s.next();
        Teacher tea = new Teacher(name,id,age,year);
        return tea;
    }
}
